package com.lori.netshop.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lori.netshop.pojo.Iteminfo;
import com.lori.netshop.pojo.SolrResult;
import com.lori.netshop.service.ItemService;

@Controller
public class ItemController {
	//注入service
	@Autowired
	private ItemService itemServiceimp;
	/**
	 * 商品搜索
	 * @param queryString
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping("/search")
	public String showItem(@RequestParam("q")String queryString,@RequestParam(defaultValue="1") int page,Model model){
		//设置get的乱码问题
		try {
			queryString = new String(queryString.getBytes("ISO-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SolrResult solrresult = itemServiceimp.findItem(queryString, page);
		model.addAttribute("query", queryString);
		model.addAttribute("totalPages",solrresult.getPageTotal()+"");
		model.addAttribute("itemList", solrresult.getItemList());
		return "search";
	}
	/**
	 * 商品基本信息展示
	 * @param itemId
	 * @param model
	 * @return
	 */
	@RequestMapping("/item/{id}")
	public String showIteminfo(@PathVariable("id")long itemId,Model model){
		Iteminfo iteminfo = itemServiceimp.findIteminfo(itemId);
		if(iteminfo != null){
			model.addAttribute("item", iteminfo);
		}
		return "item";
	}
	/**
	 * 商品详细信息的展示
	 * @param itemid
	 * @return
	 */
	@RequestMapping(value="/item/desc/{itemid}", produces=MediaType.TEXT_HTML_VALUE+";charset=utf-8")
	@ResponseBody
	public String findItemdesc(@PathVariable("itemid")long itemid){
		String itemdesc = itemServiceimp.findItemdesc(itemid);
		return itemdesc;
	}
	@RequestMapping(value="/item/param/{itemid}", produces=MediaType.TEXT_HTML_VALUE+";charset=utf-8")
	@ResponseBody
	public String findItemparam(@PathVariable("itemid")long itemid){
		String itemparam = itemServiceimp.findItemparam(itemid);
		return itemparam;
	}
	
}
